package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.OutboundItems;
import com.ruoyi.system.mapper.OutboundsMapper;
import com.ruoyi.system.domain.Outbounds;
import com.ruoyi.system.service.IOutboundsService;

/**
 *  出库单Service业务层处理
 * 
 * @author he
 * @date 2025-09-22
 */
@Service
public class OutboundsServiceImpl implements IOutboundsService 
{
    @Autowired
    private OutboundsMapper outboundsMapper;

    /**
     * 查询 出库单
     * 
     * @param id  出库单主键
     * @return  出库单
     */
    @Override
    public Outbounds selectOutboundsById(Long id)
    {
        return outboundsMapper.selectOutboundsById(id);
    }

    /**
     * 查询 出库单列表
     * 
     * @param outbounds  出库单
     * @return  出库单
     */
    @Override
    public List<Outbounds> selectOutboundsList(Outbounds outbounds)
    {
        return outboundsMapper.selectOutboundsList(outbounds);
    }

    /**
     * 新增 出库单
     * 
     * @param outbounds  出库单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOutbounds(Outbounds outbounds)
    {
        outbounds.setCreateTime(DateUtils.getNowDate());
    outbounds.setCreateUserId(SecurityUtils.getUserId());
    outbounds.setCreateUserName(SecurityUtils.getUsername());
        int rows = outboundsMapper.insertOutbounds(outbounds);
        insertOutboundItems(outbounds);
        return rows;
    }

    /**
     * 修改 出库单
     * 
     * @param outbounds  出库单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOutbounds(Outbounds outbounds)
    {
        outbounds.setUpdateTime(DateUtils.getNowDate());
    outbounds.setUpdateUserId(SecurityUtils.getUserId());
    outbounds.setUpdateUserName(SecurityUtils.getUsername());
        outboundsMapper.deleteOutboundItemsByInterId(outbounds.getId());
        insertOutboundItems(outbounds);
        return outboundsMapper.updateOutbounds(outbounds);
    }

    /**
     * 批量删除 出库单
     * 
     * @param ids 需要删除的 出库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOutboundsByIds(Long[] ids)
    {
        outboundsMapper.deleteOutboundItemsByInterIds(ids);
        return outboundsMapper.deleteOutboundsByIds(ids);
    }

    /**
     * 删除 出库单信息
     * 
     * @param id  出库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOutboundsById(Long id)
    {
        outboundsMapper.deleteOutboundItemsByInterId(id);
        return outboundsMapper.deleteOutboundsById(id);
    }

    /**
     * 新增出库单项信息
     * 
     * @param outbounds  出库单对象
     */
    public void insertOutboundItems(Outbounds outbounds)
    {
        List<OutboundItems> outboundItemsList = outbounds.getOutboundItemsList();
        Long id = outbounds.getId();
        if (StringUtils.isNotNull(outboundItemsList))
        {
            List<OutboundItems> list = new ArrayList<OutboundItems>();
            for (OutboundItems outboundItems : outboundItemsList)
            {
                outboundItems.setInterId(id);
                list.add(outboundItems);
            }
            if (list.size() > 0)
            {
                outboundsMapper.batchOutboundItems(list);
            }
        }
    }
}
